package com.offcn.controller;

import com.offcn.bean.Btype;
import com.offcn.service.BtypeService;
import com.offcn.utils.ReturnResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("btype")
@ResponseBody
public class BtypeController {
    private ReturnResult returnResult;
    @Autowired
    private BtypeService btypeService;

    //删除二级类别
    @RequestMapping("deleteTwoBtype")
    public ReturnResult deleteTwoBtype(Integer typeid){
        return btypeService.deleteTwoBtype(typeid);
    }

    //删除一级类别
    @RequestMapping("deleteOneBtype/{typeid}")
    public ReturnResult deleteOneBtype(@PathVariable(value = "typeid") Integer typeid){
        return btypeService.deleteOneBtype(typeid);
    }

    //修改一级类别-修改
    @RequestMapping("updateBtype")
    public ReturnResult updateBtype(Btype btype){
        System.out.println(btype);
        try {
            btypeService.updateBtype(btype);
            return new ReturnResult("【"+btype.getTypename()+"】修改成功！",1,null);
        } catch (Exception e) {
            e.printStackTrace();
            return new ReturnResult("修改失败！",-1,null);
        }
    }

    //修改一级类别-展示要修改的信息
    @RequestMapping("getBtypeById")
    public Btype getBtypeById(Integer typeid){
        Btype btypeById = btypeService.getBtypeById(typeid);
        return btypeById;
    }

    //添加一级类别
    @RequestMapping("addBtype")
    public ReturnResult addBtype(Btype btype){
        try {
            btypeService.addBtype(btype);
            returnResult = new ReturnResult("【"+btype.getTypename()+"】添加成功!",1,null);
        } catch (Exception e) {
            e.printStackTrace();
            returnResult = new ReturnResult("【"+btype.getTypename()+"】添加成功!",1,null);
        }
        return returnResult;
    }

    //查询大类别
    @RequestMapping("getBigType")
    public List<Btype> getBigType(){
        List<Btype> bigType = btypeService.getBigType();
        return bigType;
    }

    //查询二级类别
    @RequestMapping("getsType")
    public List<Btype> getsType(Integer typeId){
        System.out.println(typeId);
        List<Btype> sType = btypeService.getsType(typeId);
        return sType;
    }
}
